"""
结果查看页面
"""
import streamlit as st
from ai.evaluator import Evaluator
from data.exporters.report_exporter import ReportExporter


def show():
    """
    显示结果查看页面
    """
    st.title("测算结果")
    
    # 检查测算结果
    if "calculation_result" not in st.session_state:
        st.warning("请先执行测算")
        return
    
    result = st.session_state.calculation_result
    
    # 结果可视化
    st.subheader("结果概览")
    col1, col2, col3 = st.columns(3)
    with col1:
        st.metric("总收益", f"¥{result.get('total_profit', 0):,.2f}")
    with col2:
        st.metric("投资回报率", f"{result.get('roi', 0)*100:.2f}%")
    with col3:
        st.metric("盈亏平衡点", f"{result.get('break_even', 0)}个月")
    
    # 详细结果
    st.subheader("详细数据")
    st.dataframe(result.get("detailed_data", {}))
    
    # 图表展示
    st.subheader("趋势分析")
    chart_type = st.selectbox(
        "选择图表类型",
        options=["折线图", "柱状图", "饼图"]
    )
    
    # 根据选择渲染图表
    if chart_type == "折线图":
        st.line_chart(result.get("trend_data", {}))
    elif chart_type == "柱状图":
        st.bar_chart(result.get("trend_data", {}))
    else:
        st.area_chart(result.get("trend_data", {}))
    
    # AI 评价
    st.subheader("智能评价")
    if st.button("生成评价报告"):
        evaluator = Evaluator()
        scene_data = {
            "name": st.session_state.current_scene.name,
            "type": st.session_state.current_scene.scene_type,
            "parameters": st.session_state.parameters
        }
        evaluation = evaluator.evaluate(scene_data, result)
        st.session_state.evaluation_report = evaluation
    
    if "evaluation_report" in st.session_state:
        st.write(st.session_state.evaluation_report["evaluation"])
        
        st.subheader("关键指标")
        st.json(st.session_state.evaluation_report["key_metrics"])
        
        st.subheader("优化建议")
        for rec in st.session_state.evaluation_report["recommendations"]:
            st.write(f"- {rec}")
    
    # 导出功能
    st.subheader("导出结果")
    export_format = st.selectbox(
        "选择导出格式",
        options=["Excel", "PDF报告"]
    )
    
    if st.button("导出"):
        exporter = ReportExporter()
        if export_format == "Excel":
            file_path = exporter.export_to_excel(result)
            st.success(f"Excel 文件已导出到: {file_path}")
        else:
            file_path = exporter.export_to_pdf(result)
            st.success(f"PDF 报告已导出到: {file_path}")